package gov.fema.hazus.bdc.server.servlet;

import gov.fema.hazus.bdc.client.common.BuildingQuery;
import gov.fema.hazus.bdc.client.common.UserQuery;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.User;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.googlecode.objectify.OKey;
import com.googlecode.objectify.OQuery;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class UserStatistics {

	private final Logger logger = Logger.getLogger(UserStatistics.class);
	private DAO dao = new DAO();
	
	public UserStatistics()
	{
		
	}

	public int getCount()
	{
		Objectify ofy = ObjectifyService.begin();
		OQuery<User> q = ObjectifyService.createQuery(User.class);
		ArrayList<OKey<User>> users = (ArrayList<OKey<User>>) ofy.prepareKeysOnly(q).asList(FetchOptions.Builder.withLimit(Integer.MAX_VALUE).offset(0));
		return users.size();
	}
	
	public int getSearchCount(UserQuery query)
	{
		
		Objectify ofy = ObjectifyService.begin();
		OQuery<User> q = ObjectifyService.createQuery(User.class);
		q.sort("name"); // ascending sort on building name
		q.filter("username == ", query.getSearchTerm());
		ArrayList<OKey<User>> users = (ArrayList<OKey<User>>) ofy.prepareKeysOnly(q).asList(FetchOptions.Builder.withLimit(Integer.MAX_VALUE).offset(0));
		return users.size();
	}
	
}
